import 'package:xvideos/hj_page/mine_business/ai/mode/AiModList.dart';
import 'package:xvideos/hj_utils/api_service/ai_service.dart';
import 'package:xvideos/tools_base/base_list_controller.dart';

class AIFaceChangeLogic extends ListBaseLogic {
  final AICategoryMod? mod;
  final int type; //0-图片 /1-视频
  List<String> sortType = ['上架时间', '使用次数', '价格排序']; //排序

  int sortIndex = 0; //排序 0-上架时间, 1-使用次数, 2-价格排序
  bool ascending = true; //是否是升序

  int get crossAxisCount => type == 1 ? 2 : 3;

  double get spacing => type == 1 ? 7 : 5;

  double get ratio => type == 1 ? 168 / 95 : 111 / 148;

  AIFaceChangeLogic({this.mod, this.type = 0});

  @override
  void onReady() {
    super.onReady();
    loadData();
  }

  loadData() async {
    dataList ??= [];
    final resp = await AIService.getAIChangeFaceList(type: type, categoryId: mod?.id ?? '');
    if (resp != null) {
      dataList?.addAll(resp.templateList ?? []);
      //默认上架时间升序
      dataList?.sort((obj1, obj2) => (obj1.timestamp).compareTo(obj2.timestamp));
    }
    update();
  }

  //排序
  changeSortAction(int index) {
    if (index == sortIndex) {
      ascending = !ascending;
    } else {
      sortIndex = index;
      ascending = true;
    }
    //本地排序
    switch (sortIndex) {
      case 0:
        dataList?.sort(
          (obj1, obj2) => ascending ? (obj1.timestamp).compareTo(obj2.timestamp) : (obj2.timestamp).compareTo(obj1.timestamp),
        );
      case 1:
        dataList?.sort(
          (obj1, obj2) => ascending ? (obj1.usedCount).compareTo(obj2.usedCount) : (obj2.usedCount).compareTo(obj1.usedCount),
        );
      case 2:
        dataList?.sort(
          (obj1, obj2) => ascending ? (obj1.coin).compareTo(obj2.coin) : (obj2.coin).compareTo(obj1.coin),
        );
        break;
      default:
    }
    update();
  }
}
